package com.ruoyi.activity.service.impl;

import java.util.List;

import com.ruoyi.activity.req.AccountLoginReq;
import com.ruoyi.activity.req.AccountReq;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.activity.mapper.AccountMapper;
import com.ruoyi.activity.domain.Account;
import com.ruoyi.activity.service.IAccountService;

/**
 * 账号Service业务层处理
 *
 * @author wxc
 * @date 2024-05-29
 */
@Service
public class AccountServiceImpl implements IAccountService
{
    @Autowired
    private AccountMapper accountMapper;

    /**
     * 查询账号
     *
     * @param id 账号主键
     * @return 账号
     */
    @Override
    public Account selectAccountById(Long id)
    {
        return accountMapper.selectAccountById(id);
    }

    /**
     * 查询账号列表
     *
     * @param account 账号
     * @return 账号
     */
    @Override
    public List<Account> selectAccountList(Account account)
    {
        return accountMapper.selectAccountList(account);
    }

    /**
     * 新增账号
     *
     * @param req 账号
     * @return 结果
     */
    @Override
    public int insertAccount(AccountReq req)
    {
        req.setCreateTime(DateUtils.getNowDate());
        return accountMapper.insertAccount(req);
    }

    /**
     * 修改账号
     *
     * @param account 账号
     * @return 结果
     */
    @Override
    public int updateAccount(AccountReq req)
    {
        req.setUpdateTime(DateUtils.getNowDate());
        return accountMapper.updateAccount(req);
    }

    /**
     * 批量删除账号
     *
     * @param ids 需要删除的账号主键
     * @return 结果
     */
    @Override
    public int deleteAccountByIds(Long[] ids)
    {
        return accountMapper.deleteAccountByIds(ids);
    }

    /**
     * 删除账号信息
     *
     * @param id 账号主键
     * @return 结果
     */
    @Override
    public int deleteAccountById(Long id)
    {
        return accountMapper.deleteAccountById(id);
    }

    /**
     * 批量插入账号
     * @param accountList
     * @return
     */
    @Override
    public int insertAccountList(List<Account> accountList) {
        return accountMapper.insertAccountList(accountList);
    }

    /**
     * 登录
     * @param req
     * @return
     */
    @Override
    public String login(AccountLoginReq req) {
        Account account = new Account();
        BeanUtils.copyProperties(req,account);
        List<Account> accounts = selectAccountList(account);
        if(accounts.size()>0){
            String token = IdUtils.fastUUID();
            return token;
        }else {
            throw new RuntimeException("账号或密码错误");
        }

    }
}
